﻿using System;
using System.Collections.Generic;
using System.IO;
using PPT = PowerPoint;

namespace SubCom_Doc2Pdf
{
    internal class Ppt2PdfViaWPS : Convertor
    {
        public Ppt2PdfViaWPS(Target target) : base(target)
        {
            List<string> list = new List<string>();
            list.Add("wpp");
            SetTargetApps(list);   
        }

        protected override void OnProcess(Target target)
        {
            string src = target.Sources;
            string dest = target.Destination;

            PPT.Application app = new PPT.Application();
            app.DisplayAlerts = PPT.PpAlertLevel.ppAlertsNone;
            PPT.Presentation presentation = null;
            try
            {
                presentation = app.Presentations.Open(@src,PPT.MsoTriState.msoCTrue,PPT.MsoTriState.msoFalse,PPT.MsoTriState.msoFalse);
                presentation.ExportAsFixedFormat(@dest == null ? @src + ".pdf" : dest,
                    PPT.PpFixedFormatType.ppFixedFormatTypePDF);
                Console.WriteLine(SUCCESS);
            }
            catch (Exception ex)
            {
                ThrowException(ex);
            }
            finally
            {
                try
                {
                    if (presentation != null)
                    {
                        presentation.Close();
                    }
                }
                catch (Exception ex)
                {
                    ThrowException(ex);
                }
                app.Quit();
            }
        }
    }
}